package structure_pattern.proxy_pattern;

/**
 * @author fly
 * @data 2022-04-29
 * RealSearcher 代理类，实现查询数据前身份验证和，日志记录
 */
public class ProxySearcher implements Searcher {

    /**
     * 目标类
     */
    private RealSearcher realSearcher = new RealSearcher();

    /**
     * 需要增强的方法
     */
    private Logger logger;
    private AccessValidator accessValidator;

    @Override
    public void select(String user) {
        // 身份验证
        boolean validator = validator(user);
        // 查询数据
        if (validator) {
            realSearcher.select(user);
            //记录日志
            this.logSuccess(user);
        } else {
            this.logFail(user);
        }

    }

    /**
     * 实现查询前身份验证
     *
     * @param user
     * @return
     */
    public boolean validator(String user) {
        accessValidator = new AccessValidator();
        return accessValidator.Validator(user);
    }

    /**
     * 查询后记录日志
     *
     * @param user
     */
    public void logSuccess(String user) {
        Logger logger = new Logger();
        logger.logSuccess(user);
    }

    public void logFail(String user) {
        Logger logger = new Logger();
        logger.logFail(user);
    }
}